package com.ruoyi.workflow.service.impl;

import java.io.Serializable;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.BpmnNodeMapper;
import com.ruoyi.workflow.domain.BpmnNode;
import com.ruoyi.workflow.service.IBpmnNodeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程节点信息Service业务层处理
 *
 * @author xiaochen
 * @date 2022-11-17
 */
@Service
public class BpmnNodeServiceImpl extends ServiceImpl<BpmnNodeMapper, BpmnNode> implements IBpmnNodeService {

    /**
     * 查询流程节点信息列表
     *
     * @param bpmnNode 流程节点信息
     * @return 流程节点信息
     */
    @Override
    public List<BpmnNode> selectBpmnNodeList(BpmnNode bpmnNode) {
        return getBaseMapper().selectBpmnNodeList(bpmnNode);
    }

    @Override
    public List<SysUser> selectCandidateByTaskKey(String taskDefinitionKey) {
        return getBaseMapper().selectCandidateByTaskKey(taskDefinitionKey);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(BpmnNode entity) {
        boolean save = super.save(entity);
        getBaseMapper().addRelation(entity.getId(), entity.getCandidates());
        return save;
    }

    @Override
    public boolean updateById(BpmnNode entity) {
        getBaseMapper().removeRelation(entity.getId());
        boolean save = super.updateById(entity);
        getBaseMapper().addRelation(entity.getId(), entity.getCandidates());
        return save;
    }

    @Override
    public BpmnNode getById(Serializable id) {
        BpmnNode bpmnNode = super.getById(id);
        List<Long> list =  getBaseMapper().selectCandidatesById(id);
        bpmnNode.setCandidates(list);
        return bpmnNode;
    }
}
